<?php

namespace app\common;

trait Tools
{
    /**
     * 按百分比截取中文字符串
     */
    public function mb_substr_percentage($str, $start, $percentage)
    {
        $percentage = $percentage > 0 ? $percentage : abs($percentage);
        $str_leng =  floor(mb_strlen($str) / $percentage);
        $str_leng = ($str_leng > 0) ? $str_leng : '-' . $str_leng;
        return mb_substr($str, $start, $str_leng);
    }

    /**
     * 获取目录及目录下面所有文件路径
     */
    public function searchDir($path, &$data, $is_path_info = false)
    {
        if (is_dir($path)) {
            $dp = dir($path);
            while ($file = $dp->read()) {
                if ($file != '.' && $file != '..') {
                    $this->searchDir($path . DIRECTORY_SEPARATOR . $file, $data, $is_path_info);
                }
            }
            $dp->close();
        }
        if (is_file($path)) {
            if ($is_path_info) {
                $file_info = pathinfo($path);
                $file_info['filepath'] = $path;
                $file_info['size'] = filesize($path);
                $data[] = $file_info;
            } else {
                $data[] = $path;
            }
        }
    }

    private function getDir($dir, $is_path_info = false)
    {
        $data = array();
        $this->searchDir($dir, $data, $is_path_info);
        return   $data;
    }

    public function fireinfo()
    {
        $base_path = "F:\资源整理合集\\";
        $path_list =    $this->getDir($base_path);
        foreach ($path_list as $key => $file) {
            $file_info = pathinfo($file);
            $data = [
                'name' => $file_info['basename'],
                'path' => $file,
                'size' => filesize($file),
            ];
            DB::table('files')->insert($data);
        }
    }
    function size($path)
    {
        //判断是否是一个文件
        if (!file_exists($path)) {
            return false;
        }
        $size = filesize($path);
        if ($size > pow(1024, 3)) {
            $fileSize = round($size / pow(1024, 3), 3) . 'GB';
        } else if ($size > pow(1024, 2)) {
            $fileSize = round($size / pow(1024, 3), 3) . 'MB';
        } else if ($size > pow(1024, 1)) {
            $fileSize = round($size / pow(1024, 1), 3) . 'KB';
        } else {
            $fileSize = $size . 'B';
        }
        return $fileSize;
    }

    function convertToFlv($input, $output)
    {
        $command = "ffmpeg -v 0 -y -i $input -vframes 1 -ss 5 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 $output ";
        shell_exec($command);
    }

    /**
     * png图文合成 by wangzhaobo
     * @param  string $pic_path   图片目录
     * @param  array $text       文字
     * @param  array $font_file  路径
     * @param  array $font_size  文字大小
     * @param  array $font_pos_y 文字距离图片高度
     * @return string             合成图片的名称
     */
    function signImg($pic_path, $text, $font_file, $font_size, $font_pos_y)
    {
        //图片信息
        list($pic_w, $pic_h, $pic_type) = getimagesize($pic_path);
        //创建图片的实例
        $pic = imagecreatefrompng($pic_path);
        imagesavealpha($pic, true); //这里很重要 意思是不要丢了图像的透明色;
        $yellow = imagecolorallocate($pic, 236, 185, 7); //默认的文字颜色
        $font_color = [$yellow, $yellow, $yellow, $yellow,];
        foreach ($text as $key => $value) {
            dump($key);
            $arr = imagettfbbox($font_size[$key], 0, $font_file[$key], $text[$key]);
            $text_width = $arr[2] - $arr[0];
            imagefttext($pic, $font_size[$key], 0, ($pic_w - $text_width) / 2, $font_pos_y[$key], $font_color[$key], $font_file[$key], $text[$key]);
        }
        //按照画布类型输出图片
        $pngName = time() . rand(0, 9) . ".png"; //生成图片名称

        switch ($pic_type) {
            case 1: //GIF

            case 2: //JPG

            case 3: //PNG
                // //直接输出图片
                // header('Content-Type: image/png');
                // imagepng($pic);

                //保存图片路径
                imagepng($pic, runtime_path() . $pngName);
                return $pngName;
                break;
            default:
                break;
        }
        imagedestroy($pic);
    }
    public function testImage()
    {
        $pic_path = '/mnt/d/source/cover_img.png';
        $text = [
            '绵阳市教育资源平台',
            '统一上传',
        ];
        $font_file = [
            "/mnt/c/Windows/Fonts/msyh.ttc", //粗体
            "/mnt/c/Windows/Fonts/msyh.ttc", //粗体
        ];
        $font_size = [24, 42,];
        $font_pos_y = [100, 200,];
        $this->signImg($pic_path, $text, $font_file, $font_size, $font_pos_y);
   
    }
}
